<?php
namespace App\Models\Chain\InviteCash;


use App\Constants\CreditLogConstant;
use App\Models\Factory\InviteFactory;
use App\Models\Chain\AbstractHandler;
use App\Models\Factory\UserCreditFactory;
use App\Models\Factory\UserInviteFactory;
use App\Models\Orm\AwUserIdentityverify;
use App\Models\Orm\AwUserIntegral;
use App\Models\Orm\AwUserIntegralLog;

/**
 * 修改用户积分表
 * Class UpdateCreditAction
 * @package App\Models\Chain\Identity
 */
class UpdateCreditAction extends AbstractHandler
{
    private $params = array();

    protected $error = array('error' => '更新积分表失败', 'code' => 7012);


    public function __construct($params)
    {
        $this->params = $params;
    }

    public function handleRequest()
    {
        $result = $this->updateCredit($this->params);

        if ($result == 1) {
            $this->setSuccessor(new CreateInviteCashAction($this->params));
            return $this->getSuccessor()->handleRequest();
        } elseif ($result == 2) {
            return true;
        } else {

            return $this->error;
        }
    }


    public function updateCredit($params)
    {

        $integral = AwUserIdentityverify::INTEGRAL_IDENTITY;
        $result = UserCreditFactory::updateCreditInvite($params['user_id'], $integral);

        if ($result) {

            //判断一下这个用户是否是被邀请的用户
            $Invite = UserInviteFactory::isInvite($params['user_id']);

            if ($Invite) {
                $params['user_id_invite'] = $Invite->user_id;
                $this->params = $params;
                return 1;
            }
            return 2;
        }

        return false;
    }


}